<template>
    <el-dialog title="退款"
               :close-on-click-modal="false"
               :visible.sync="visible"
               @close="close">
      <p>订单实际支付金额：{{order.actualAmount}}</p>
      <el-form ref="form" :model="dataForm" :rules="dataRule" label-width="100px">
        <el-form-item label="退款金额：" prop="refundAmount">
          <el-input v-model="dataForm.refundAmount" onkeyup="this.value = this.value.replace(/[^\d.]/g,'');" placeholder="请输入退款金额" clearable></el-input>
        </el-form-item>
        <el-form-item label="退款原因：">
          <el-input v-model="dataForm.reason" type="textarea" placeholder="请输入退款原因" :autosize="{ minRows: 2, maxRows: 4}" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click.stop="visible=false">取消</el-button>
          <el-button type="primary" :loading="loading" @click.stop="confirm">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
</template>

<script>
  export default {
    name: 'orderRefund',
    data () {
      var validateMoney=(rule,value,callBack)=>{
        if(parseFloat(value)==0){
          callBack(new Error('退款金额必须大于0'))
        }
        if(parseFloat(value)>parseFloat(this.order.actualAmount)){
          callBack(new Error('退款金额不能大于实际金额'))
        }
        else {
          callBack()
        }
      };
      return {
        visible: false,
        loading:false,
        order: {},
        dataForm: {
          refundAmount:'',
          reason:''
        },
        dataRule:{
          refundAmount:[{ required: true, message: '退款金额不能不能为空', trigger: 'blur' },
            { validator: validateMoney, trigger: 'blur' }]
        }
      }
    },
    methods: {
      init (model) {
        this.visible = true
        this.$nextTick(() => {
          this.loading=false
          this.dataForm.refundAmount=''
          this.dataForm.reason=''
          this.$refs['form'].clearValidate()
          this.order = model
          console.log(model)
        })
      },
      close () {
        this.visible = false
      },
      confirm(){
        this.$refs['form'].validate((valid)=>{
          if (valid){
            this.loading=true
            this.$http({
              url:this.$http.adornUrl('/v1/merchant/order/refund'),
              method:'POST',
              data:this.$http.adornData({
                orderNumber:this.order.orderNumber,
                refundAmount:this.dataForm.refundAmount,
                reason:this.dataForm.reason
              })
            }).then(({data})=>{
              this.loading=false
              this.$message({
                type:'success',
                message:'退款成功',
                duration:1500,
                onClose:()=>{
                  this.visible=false
                  this.$emit('refreshDataList')
                }
              })
            }).catch((error)=>{
              this.loading=false
            })
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
